package com.zsk.service.impl;

import java.util.List;

import org.springframework.stereotype.Service;

import com.zsk.bean.TableParams;
import com.zsk.service.IRepositoryCodeGenService;
import com.zsk.util.FileUtil;
import com.zsk.util.MvnPrjUtil;

@Service
public class RepositoryCodeGenServiceImpl implements IRepositoryCodeGenService {

	@Override
	public boolean genRepositoryCode(List<TableParams> list, String packageName, String groupId, String artifactId) {

		for (TableParams tableInfo : list)
			genRepository(tableInfo, packageName, artifactId);
		return true;
	}

	protected static void genRepository(TableParams tableInfo, String packageName, String artifactId) {
		String name = "repository";
		String nameExt = "Repository";
		String dir = MvnPrjUtil.getWorkCodeDir(artifactId, packageName) + name;
		String tableName = tableInfo.getTableName();

		StringBuilder sb = FileUtil.readTxtFile("template/RepositoryTemplate.java");
		String className = MvnPrjUtil.getClassName(tableName);
		String colName = tableInfo.getTableCol().get(0).getColName();
		colName = colName.substring(0, 1).toUpperCase() + colName.substring(1);
		String content = sb.toString().replace("#1", packageName + "." + name)
				.replace("#2", packageName + ".entity." + className + "Entity;")
				.replace("#3", className + "Entity" + nameExt).replace("#4", className + "Entity")
				.replace("#5", colName).replace("#6", colName).replace("#7", colName);

		FileUtil.makeDir(dir);
		FileUtil.writeTxtFile(content, dir + "/" + className + "Entity" + nameExt + ".java");
	}

}
